EVote = (EVote or {});
EVote.enabled = (EVote.enabled or true);
EVote.mode = (EVote.mode or 'rp');
EVote.answer = {};
EVote.question = {};

local timer_active;
local start_time;
local warning_sent = false;
local EVote_results = {};
local EVote_total = 0;
local EVote_totals = {};

local EVote_channel_default = "RAID";

SLASH_EVote1 = "/EVote";

SlashCmdList["EVote"] = function(msg)
	argv = {};
	for arg in string.gmatch(msg, '[%a%d%-%.%?]+') do
		table.insert(argv, arg);
	end
	
	if argv[1] == "on" then
		EVote.enabled = true;
		EVote_ChatMsg("Forsooth is enabled");
	elseif argv[1] == "foo" then
		EVote_ChatMsg(EVote.answer[1]);
	elseif argv[1] == "off" then
		EVote.enabled = false;
		EVote_ChatMsg("Forsooth is disabled");
	elseif argv[1] == "question" or argv[1] == "q" then
		if argv[2] then
			local question = "";
			for i=2,table.getn(argv) do
				question=question.." "..argv[i];
			end
			question = string.gsub(question, "^ *", "");
			
			EVote.question = question;
			EVote_ChatMsg("Question set to: "..EVote.question);
		else
			EVote_ChatWarning("Usage: /EVote question <question>");
		end
	elseif argv[1] == "answer" or argv[1] == "a" then
		if argv[2] then
			local answer = ""
			for i=2,table.getn(argv) do
				answer=answer.." "..argv[i];
			end
			
			answer = string.gsub(answer, "^ *", "");
			table.insert(EVote.answer, answer);
			EVote_ChatMsg("Added Answer: "..answer);
		else
			EVote_ChatWarning("Usage: /EVote answer <number> <answer>");
		end
	elseif argv[1] == "reset" then
		if argv[2] and argv[2]=="question" then
			EVote.question = {};
			EVote_ChatMsg("Questions Reset");
		elseif argv[2] and argv[2]=="answers" then
			EVote.answer = {};
			EVote_ChatMsg("Answers Reset");
		elseif argv[2] then
			EVote_ChatError("Unknown argument to reset");
			EVote_ChatError("Usage:");
			EVote_ChatError("/EVote reset");
			EVote_ChatError("/EVote reset question");
			EVote_ChatError("/EVote reset answers");
		else
			EVote.answer = {};
			EVote.question = {};
			EVote_ChatMsg("Questions and Answers Reset");
		end
	elseif argv[1] == "test" then
		EVote_ChatMsg("A vote has been requested:");
		EVote_ChatMsg(EVote.question);
		
		for i,ans in ipairs(EVote.answer) do
			EVote_ChatMsg(i..": "..ans);
		end
	elseif argv[1] == "cast" then
		if argv[2] then
			EVote_channel = argv[2];
		else
			EVote_channel = EVote_channel_default;
		end
		EVote_start();
	elseif argv[1] == "stop" then
		if (timer_active) then
			SendChatMessage("<-EVote-> The vote has completed.", EVote_channel);
			EVote_stop();
		end
	else
		EVote_ChatMsg("EVote:");
		EVote_ChatMsg("/EVote on -- Enable Forsooth");
		EVote_ChatMsg("/EVote off -- Disable Forsooth");
		EVote_ChatMsg("/EVote question <question> -- Set the question asked by EVote");
		EVote_ChatMsg("/EVote answer <number> <answer> -- Set the valid answer for number");
		EVote_ChatMsg("/EVote cast -- Start the EVote");
		EVote_ChatMsg("/EVote stop -- Cancel the EVote");
		EVote_ChatMsg("/EVote test -- Send a test EVote to yourself");
		EVote_ChatMsg("/EVote reset -- Clear all questions and answers");
		EVote_ChatMsg("/EVote reset question -- Clear question");
		EVote_ChatMsg("/EVote reset answers -- Clear all answers");
		EVote_ChatMsg("/EVote reset answer <number> -- Clear answer <number>");
	end
end

function EVote_OnLoad()
	this:RegisterEvent("CHAT_MSG_WHISPER");
	this:RegisterEvent("CHAT_MSG_RAID");
	EVoteFrame:Hide();
end

function EVote_OnUpdate()
	if(timer_active and EVote.enabled) then
		local time_running = math.floor(GetTime()-startTime);
		local time_remaining = 45 - time_running;
		EVoteStatusBar:SetValue(45 - GetTime()-startTime);
		EVoteText:SetText(time_remaining.." Seconds");

		if (time_remaining <= 15) and (not warning_sent) then
			SendChatMessage("<-EVote-> This vote will complete in 15 seconds.", EVote_channel);
			warning_sent = true;

		end;
		if (time_remaining <= 0) then
			EVote_stop();
		end;
	end;
end

-- An all purpose event handler.
function EVote_event(event)
	if (event == "CHAT_MSG_RAID" or event == "CHAT_MSG_WHISPER") and string.find(arg1, "^%d+$") and timer_active then
		local Vote = tonumber(arg1);
		if EVote.answer[Vote] == nil then
			SendChatMessage("<-EVote-> Your vote ("..arg1..") was not recognized", "WHISPER", GetDefaultLanguage("player"), arg2);
			for i,ans in ipairs(EVote.answer) do
				EVote_ChatMsg("["..i.."]: ".."["..ans.."]");
			end

		else
			EVote_results[arg2] = Vote;
			EVote_total = EVote_total+1;
			if EVote_totals[Vote] == nil then EVote_totals[Vote] = 0 end;
			EVote_totals[Vote]=EVote_totals[Vote]+1;
			EVote_ChatMsg(arg2.." voted "..EVote_results[arg2]);
			SendChatMessage("<-EVote-> You voted: "..EVote_results[arg2], "WHISPER", GetDefaultLanguage("player"), arg2);
		end
	end
end

function EVote_start()
	SendChatMessage("<-EVote-> A vote has been requested...", EVote_channel);
	SendChatMessage("<-EVote-> This vote will time out in 45 seconds. Please whisper the number corresponding to your answer. Only your latest vote will be counted", EVote_channel);
	SendChatMessage("<-EVote-> " ..EVote.question, EVote_channel);

	for i,ans in ipairs(EVote.answer) do
		SendChatMessage("<-EVote-> " ..i..": "..ans, EVote_channel);
	end

	EVote_ChatMsg("<-EVote-> Starting vote...");
	EVote_results = {};
	EVote_total = 0;
	EVote_totals = {};
	startTime = GetTime();
	EVoteStatusBar:SetMinMaxValues(0,45);
	EVoteStatusBar:SetValue(45);
	EVoteText:SetText("45 Seconds");
	EVoteFrame:Show();
	timer_active = true;
end;

function EVote_stop()
	timer_active = false;
	EVoteFrame:Hide();
	SendChatMessage("<-EVote-> The vote has completed.", EVote_channel);
	for key,value in pairs(EVote_totals) do
		local percent = math.floor((value/EVote_total)*1000)/10;
		local answers = EVote.answer;
		
		SendChatMessage("<-EVote-> " ..key..": "..value..") "..EVote.answer[key].." ("..percent.."%)", EVote_channel);
	end
	
	EVote_results = {};
end;

function EVote_ChatMsg(msg)
    if(DEFAULT_CHAT_FRAME and msg) then
        DEFAULT_CHAT_FRAME:AddMessage("|cffffffff" .. msg);
    end
end

function EVote_ChatWarning(msg)
    if(DEFAULT_CHAT_FRAME and msg) then
        DEFAULT_CHAT_FRAME:AddMessage(msg, 1.0, 1.0, 0.0);
    end
end

function EVote_ChatError(msg)
    if(DEFAULT_CHAT_FRAME and msg) then
        DEFAULT_CHAT_FRAME:AddMessage(msg, 1.0, 0.0, 0.0);
    end
end